local start, duration = GetSpellCooldown("Rend");
local gcd = start + duration;
local function GetCooldown(id)
  local start, duration = GetSpellCooldown(id);
  local cd = start + duration - gcd;
  if cd < 0 then
    return 0 
  else
    return cd
  end
end

--1-) Check my stance. If I'm not on Defensive Stance, I should
--switch to it.

if (GetBonusBarOffset() ~= 2) then
  return IconSpell("Defensive Stance");
end

local _, _, start, duration = IconAura("HARMFUL|PLAYER", "target", "Rend");
--I'm using 3 seconds remaining time to display thunder clap icon.
--That's because I almost always use an ability, and check these
--addons to use my "next" ability. Therefore, I'll not miss
--thunder clap, if it is shown for 3 seconds.
local rendMinDuration = 3;
--2-) Check if my target has rend, if not, cast it.
--3-) If my target's rend debuff is expiring, refresh it with 
--thunder clap.
if (not duration) then
  return IconSpell("Rend");
elseif (start + duration - GetTime()<rendMinDuration) then
  return IconSpell("Thunder Clap");
end
--I named the variable sundersToRemain, however, it is also 3 
--stacks for Fearie Fire (Feral) & Fearie Fire. Expose Armor
--is directly %12 armor reduction. Therefore, if it is on
--my target, no need to check others.
local sundersToRemain = 3;
local _, _, _, duration = IconAura("HARMFUL", "target", "Expose Armor");
if (not duration) then
  local _, _, _, countAR1 = UnitDebuff("target", "Sunder Armor");
  local _, _, _, countAR2 = UnitDebuff("target", "Faerie Fire (Feral)");
  local _, _, _, countAR3 = UnitDebuff("target", "Faerie Fire");
  --4-) If my target does not have armor reduction to it's 
  --maximum, spam devastate for 3 sunder stacks.
  if (not (countAR1 == sundersToRemain or countAR2 == sundersToRemain or countAR3 == sundersToRemain)) then
    return IconSpell("Devastate");
  end
end
--Alright, everything I should keep up is there. So I need
--to use stuff for instant damage now:
local shieldslamProcPrioDuration = 2;
local cdSS = GetCooldown("Shield Slam");
if (cdSS<=.2) then
  --5-) If my shield slam is available, in my target's faaaceee!
  return IconSpell("Shield Slam");
elseif (cdSS < shieldslamProcPrioDuration) then
  --For that last cast, before shield slam is ready, protection
  --warriors ought to use
  --a damaging ability. The order of tps is as follows:
  --Shockwave > Concussion Blow > Thunder Clap
  --I could also put heroic throw here, however, I prefer to save it
  --for necessities.
  local cdSW = GetCooldown("Shockwave");
  local cdCB = GetCooldown("Concussion Blow");
  local cdTC = GetCooldown("Thunder Clap");
  if (cdSW == 0) then
    return IconSpell("Shockwave");
  elseif (cdCB == 0) then
    return IconSpell("Concussion Blow");
  elseif (cdTC == 0) then
    return IconSpell("Thunder Clap");
  end
else
  --7-) If my shield slam cooldown is over 2 seconds, use revenge
  --to trigger Sword & Board.
  --8-) Else, devastate.
  local cdRV = GetCooldown("Revenge");
  if (cdRV == 0) then
    return IconSpell("Revenge");
  else
    return IconSpell("Devastate");
  end
end
